package com.yndsoft.jc.ui.net;

import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.model.TestModel;
import com.yndsoft.jc.ui.homepage.AdvertBean;
import com.yndsoft.jc.ui.homepage.InfoBean;
import com.yndsoft.jc.ui.net.util.RetrofitUtil;
import com.yndsoft.jc.ui.shoppingcart.entity.CartBean;

import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class HttpUtil {

    private static HttpUtil httpUtil = new HttpUtil();
    private ApiService apiService;

    private HttpUtil() {
        apiService = RetrofitUtil.getInstance().getApiService();
    }

    public static HttpUtil getInstance() {
        return httpUtil;
    }

    private <T> void toSubscribe(Subscriber<T> subscriber, Observable<T> observable) {
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    /**
     * 首页广告位
     */

    public void getData(Subscriber<ReturnMsg> subscriber, String type, int pid) {
        toSubscribe(subscriber, apiService.getData(type, pid));
    }

    /**
     * 分类页面数据
     *
     * @param subscriber
     * @param type
     */
    public void getCategoryData(Subscriber<ReturnMsg> subscriber, String type) {
        toSubscribe(subscriber, apiService.getCategoryData(type));
    }

    /**
     * 产品列表
     *
     * @param subscriber
     * @param type
     * @param pid
     * @param index
     * @param size
     */
    public void getProductListData(Subscriber<ReturnMsg> subscriber, String type, int pid, int index, int size) {
        toSubscribe(subscriber, apiService.getProductListData(type, pid, index, size));
    }

    /**
     * 获取推荐搜索
     *
     * @param subscriber
     * @param type
     */
    public void getRecommendSearch(Subscriber<ReturnMsg> subscriber, String type) {
        toSubscribe(subscriber, apiService.getRecommendSearch(type));
    }

    /**
     * 会员获取购物车
     *
     * @param subscriber
     * @param type
     * @param mid
     */
    public void getMemberCart(Subscriber<ReturnMsg> subscriber, String type, int mid) {
        toSubscribe(subscriber, apiService.getMemberCart(type, mid));
    }

    /**
     * 搜索产品列表
     *
     * @param subscriber
     * @param type
     * @param search
     */
    public void searchProductList(Subscriber<ReturnMsg> subscriber, String type, String search, int index) {
        toSubscribe(subscriber, apiService.searchProductList(type, search, index, 10));
    }

    /**
     * 获取产品详情
     *
     * @param subscriber
     * @param type
     * @param id
     */
    public void getProductDetail(Subscriber<ReturnMsg> subscriber, String type, int id) {
        toSubscribe(subscriber, apiService.getProductDetail(type, id));
    }

    /**
     * 获取产品属性
     *
     * @param subscriber
     * @param type
     * @param id
     */
    public void getProductAttribute(Subscriber<ReturnMsg> subscriber, String type, int id) {
        toSubscribe(subscriber, apiService.getProductAttribute(type, id));
    }

    /**
     * 会员产品加入购物车
     *
     * @param subscriber
     * @param type
     * @param mid
     * @param list
     */
    public void addToCart(Subscriber<ReturnMsg> subscriber, String type, int mid, String list) {
        toSubscribe(subscriber, apiService.addToCart(type, mid, list));
    }

    /**
     * 会员删除购物车列表
     *
     * @param subscriber
     * @param type
     * @param mid
     * @param json
     */
    public void deleteCartItem(Subscriber<ReturnMsg> subscriber, String type, int mid, String json) {
        toSubscribe(subscriber, apiService.deleteCartItem(type, mid, json));
    }

    /**
     * 会员获取订单列表
     *
     * @param subscriber
     * @param type
     * @param mid
     * @param tid
     * @param index
     * @param size
     */
    public void getMyOrder(Subscriber<ReturnMsg> subscriber, String type, int mid, int tid, int index, int size) {
        toSubscribe(subscriber, apiService.getMyOrder(type, mid, tid, index, size));
    }

    /**
     * 会员获取订单详情
     *
     * @param subscriber
     * @param type
     * @param mid
     * @param id
     */
    public void getOrderDetail(Subscriber<ReturnMsg> subscriber, String type, int mid, int id) {
        toSubscribe(subscriber, apiService.getOrderDetail(type, mid, id));
    }

    /**
     * 会员订单新增评论
     *
     * @param subscriber
     * @param type
     * @param mid
     * @param json
     */
    public void getOrderComment(Subscriber<ReturnMsg> subscriber, String type, int mid, String json) {
        toSubscribe(subscriber, apiService.getOrderComment(type, mid, json));
    }

    /**
     * 会员订单确认
     *
     * @param subscriber
     * @param type
     * @param mid
     * @param ctype
     * @param json
     */
    public void confirmOrder(Subscriber<ReturnMsg> subscriber, String type, int mid, int ctype, String json) {
        toSubscribe(subscriber, apiService.confirmOrder(type, mid, ctype, json));
    }

    /**
     * 会员新增寄件地址
     *
     * @param subscriber
     * @param type
     * @param json
     */
    public void postMailAddress(Subscriber<ReturnMsg> subscriber, String type, int mid, String json) {
        toSubscribe(subscriber, apiService.postMailAddress(type, mid, json));
    }

    /**
     * 会员设置默认地址
     *
     * @param subscriber
     * @param type
     * @param mid
     * @param id
     */
    public void setDefaultAds(Subscriber<ReturnMsg> subscriber, String type, int mid, int id) {
        toSubscribe(subscriber, apiService.setDefaultAds(type, mid, id));
    }

    /**
     * 会员删除寄件地址
     *
     * @param subscriber
     * @param type
     * @param mid
     * @param id
     */
    public void deleteAddress(Subscriber<ReturnMsg> subscriber, String type, int mid, int id) {
        toSubscribe(subscriber, apiService.deleteAddress(type, mid, id));
    }

    /**
     * 会员更新寄件地址
     *
     * @param subscriber
     * @param type
     * @param mid
     * @param json
     */
    public void postEditAddress(Subscriber<ReturnMsg> subscriber, String type, int mid, String json) {
        toSubscribe(subscriber, apiService.postEditAddress(type, mid, json));
    }

    /**
     * 获取会员详情
     *
     * @param subscriber
     * @param type
     * @param mid
     */
    public void getPersonInfo(Subscriber<ReturnMsg> subscriber, String type, int mid) {
        toSubscribe(subscriber, apiService.getPersonInfo(type, mid));
    }

    /**
     * 会员寄件地址
     *
     * @param subscriber
     * @param type
     * @param mid
     */
    public void getMailAds(Subscriber<ReturnMsg> subscriber, String type, int mid) {
        toSubscribe(subscriber, apiService.getMailAds(type, mid));
    }

    /**
     * 帮助类别
     *
     * @param subscriber
     * @param type
     */
    public void getHelpCategory(Subscriber<ReturnMsg> subscriber, String type) {
        toSubscribe(subscriber, apiService.getHelpCategory(type));
    }

    /**
     * 帮助列表
     *
     * @param subscriber
     * @param type
     * @param pid
     */
    public void getMonitorFlow(Subscriber<ReturnMsg> subscriber, String type, int pid) {
        toSubscribe(subscriber, apiService.getMonitorFlow(type, pid));
    }

    /**
     * 会员默认地址
     *
     * @param subscriber
     * @param type
     * @param mid
     */
    public void getDefaultAds(Subscriber<ReturnMsg> subscriber, String type, int mid) {
        toSubscribe(subscriber, apiService.getDefaultAds(type, mid));
    }

    /**
     * 意见反馈
     *
     * @param subscriber
     * @param type
     * @param json
     */
    public void postSuggestions(Subscriber<ReturnMsg> subscriber, String type, String json) {
        toSubscribe(subscriber, apiService.postSuggestions(type, json));
    }

    /**
     * 更新会员
     *
     * @param subscriber
     * @param type
     * @param json
     */
    public void commitPersonInfo(Subscriber<ReturnMsg> subscriber, String type, String json) {
        toSubscribe(subscriber, apiService.commitPersonInfo(type, json));
    }

    /**
     * 新闻详情
     *
     * @param subscriber
     * @param type
     * @param id
     */
    public void getNews(Subscriber<ReturnMsg> subscriber, String type, int id) {
        toSubscribe(subscriber, apiService.getNews(type, id));
    }

    /**
     * 配置信息
     *
     * @param subscriber
     * @param type
     * @param id
     */
    public void getConfiguration(Subscriber<ReturnMsg> subscriber, String type, int id) {
        toSubscribe(subscriber, apiService.getConfiguration(type, id));
    }

    /**
     * 会员获取积分列表
     *
     * @param subscriber
     * @param type
     * @param mid
     * @param index
     * @param size
     */
    public void getMemberPoint(Subscriber<ReturnMsg> subscriber, String type, int mid, int index, int size) {
        toSubscribe(subscriber, apiService.getMemberPoint(type, mid, index, size));
    }


    /**
     * 会员登录
     */
    public void memberLogin(Subscriber<ReturnMsg> subscriber, String type, String phone, String pwd) {
        toSubscribe(subscriber, apiService.memberLogin(type, phone, pwd));
    }

    /**
     * 发送短信验证码
     *
     * @param subscriber
     * @param type
     * @param phone
     * @param getSms
     */
    public void getSms(Subscriber<ReturnMsg> subscriber, String type, String phone, int getSms) {
        toSubscribe(subscriber, apiService.getSms(type, phone, getSms));
    }

    /**
     * 会员注册
     *
     * @param subscriber
     * @param type
     * @param json
     */
    public void registAcc(Subscriber<ReturnMsg> subscriber, String type, String json) {
        toSubscribe(subscriber, apiService.registAcc(type, json));
    }

    /**
     * 修改密码
     *
     * @param subscriber
     * @param type
     * @param mid
     * @param newpwd
     */
    public void resettingPsw(Subscriber<ReturnMsg> subscriber, String type, int mid, String newpwd) {
        toSubscribe(subscriber, apiService.resettingPsw(type, mid, newpwd));
    }

    public void updatePwd(Subscriber<ReturnMsg> subscriber, String type, String id, String newpwd) {
        toSubscribe(subscriber, apiService.updatePwd(type, id, newpwd));
    }

    public void forgetPwd(Subscriber<ReturnMsg> subscriber, String type, String phone, String newpwd) {
        toSubscribe(subscriber, apiService.forgetPwd(type, phone, newpwd));
    }

    public void forgetSupplierPwd(Subscriber<ReturnMsg> subscriber, String type, String phone, String newpwd) {
        toSubscribe(subscriber, apiService.forgetSupplierPwd(type, phone, newpwd));
    }

    /**
     * 检测机构管理员登录
     */
    public void supplierLogin(Subscriber<ReturnMsg> subscriber, String type, String phone, String pwd) {
        toSubscribe(subscriber, apiService.supplierLogin(type, phone, pwd));
    }
}
































